//
//  AddFriendViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/11.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AddFriendViewController.h"
#import "SearchView.h"
#import "chatListCell.h"
#import "SearchViewController.h"
#import "NearbyFriendsViewController.h"
#import <ContactsUI/ContactsUI.h>
#import "AddressBookListViewController.h"

@interface AddFriendViewController ()<UITableViewDataSource,UITableViewDelegate>
/**
 tableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
/**
 searchView
 */
@property (nonatomic , strong) SearchView *searchView;

@end

@implementation AddFriendViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.baseNavView.navTitleLabel.text = @"添加豚友";
    
    [self setUpNav];
    [self setUpView];
}


#pragma mark - init method
- (void)setUpNav {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
}

- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(TJ_NavBarHeight);
        make.right.left.bottom.mas_equalTo(0);
    }];
}


- (void) initAction {
    @weakify(self);
    [self.searchView setSearchBeginBlock:^{
        @strongify(self);
        SearchViewController *vc = [[SearchViewController alloc] init];
        vc.searchType = SearchTypeAddNewFriend;
        [self.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)initData {

}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 70;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 54;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    if (section == 0) {
        UIView *searchHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 54)];

        searchHeaderView.backgroundColor = [UIColor globalBackgroundColor];
        self.searchView.frame = CGRectMake(0, 10, SCREEN_WIDTH, 34);
        [searchHeaderView addSubview:self.searchView];
        
        return searchHeaderView;
    }
    return [[UIView alloc] init];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChatListCell *cell = [ChatListCell cellWithTableView:tableView];
    [cell configAddFriendCellWithIndex:indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        [self.navigationController pushViewController:[[AddressBookListViewController alloc] init] animated:YES];
        
    } else if (indexPath.row == 1) {
        [self.navigationController pushViewController:[[NearbyFriendsViewController alloc] init] animated:YES];
    }
}

#pragma mark - target action
- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - private method



#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

- (SearchView *)searchView {
    if (!_searchView) {
        _searchView = [[SearchView alloc] init];
        _searchView.backgroundColor = [UIColor whiteColor];
    }
    return _searchView;
}
@end
